using System;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;


namespace IGlobalist.Cms.Bll
{
    public class ResourceFolderController : BaseController
    {
        private static IResourceFolder ConvertCmsObject2ResourceFolder(ICmsObject obj)
        {
            IResourceFolder resourceFolder = new ResourceFolder();
            resourceFolder.FromCmsObject(obj);
            return resourceFolder;
        }


        private static IList<IResourceFolder> ConvertListObject2ListResourceFolders(IList<ICmsObject> list)
        {
            IList<IResourceFolder> resourceFolders = new List<IResourceFolder>();
            if (list != null)
            foreach (ICmsObject obj in list)
            {
                resourceFolders.Add(ConvertCmsObject2ResourceFolder(obj));
            }

            return resourceFolders;
        }


        public static IResourceFolder GetResourceFolderTree()
        {
            IResourceFolder resourceFolder = new ResourceFolder();
            ICmsObject objRoot = _Ds.GetObjectListTree((short)CmsObjectType.ResourceFolder, false);
            resourceFolder = ConvertCmsObject2ResourceFolder(objRoot);
            PopulateList(objRoot, resourceFolder);

            return resourceFolder;
        }

        private static void PopulateList(ICmsObject objRoot, IResourceFolder resourceFolder)
        {
            resourceFolder.SubFolderList  = new ResourceFolderList();
            foreach (ICmsObject obj in objRoot.SubObjectList)
            {
                if (obj.ClassTypeID == (short)CmsObjectType.ResourceFolder)
                {
                    resourceFolder.SubFolderList.Add(ConvertCmsObject2ResourceFolder(obj));
                    PopulateList(obj, resourceFolder.SubFolderList[resourceFolder.SubFolderList.Count - 1]);
                }

            }
        }

        //---------------
        private static void PopulateBind(IResourceFolder resourceFolder, IList<IResourceFolder> list, string prefix, long resourceFolderID)
        {
            if (resourceFolder.SubFolderList!= null)
            {
                prefix += "----";
                foreach (IResourceFolder resFolder in resourceFolder.SubFolderList)
                {
                    if (resFolder.ID != resourceFolderID)
                    {
                        resFolder.Name = prefix + resFolder.Name;
                        list.Add(resFolder);
                        PopulateBind(resFolder, list, prefix, resourceFolderID);
                    }
                }
            }
        }



        public static IList<IResourceFolder> GetResourceFolderTreeForBind(long resourceFolderID)
        {
            IList<IResourceFolder> list = new List<IResourceFolder>();     
            IResourceFolder root = ResourceFolderController.GetResourceFolderTree();  
            list.Add(root);

            PopulateBind(root, list, "", resourceFolderID);

            return list;
        }     
        //-------------------------



        public static IResourceFolder GetResourceFolder(long id)
        {
            return ConvertCmsObject2ResourceFolder(_Ds.GetObject(id));
        }


        public static IList<IResourceFolder> GetResourceFolderList(long classID)
        {
            return ConvertListObject2ListResourceFolders(_Ds.GetObjectListByClassID(classID));
        }

        public static IList<IResourceFolder> GetResourceFolderListForBindTree(long classID, long currentID)
        {
            return null; // ConvertListObject2ListResourceFolders(_Ds.GetObjectListTree(classID, currentID));
        }




        public static long InsertResourceFolder(IResourceFolder resourceFolder)
        {
            long id = _Ds.InsertObject((ICmsObject)resourceFolder);
            return id;
        }

        public static void UpdateResourceFolder(IResourceFolder resourceFolder)
        {
            _Ds.UpdateObject((ICmsObject)resourceFolder);

        }

        public static void DeleteResourceFolder(long id)
        {
            _Ds.DeleteObject(id);

        }
    }
}
